<?xml version="1.0" encoding="UTF-8" ?>   
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 生成文件不允许修改-->
<mapper namespace="${table.pag}.dao.${table.className}Dao" >
	<#assign beanAlias=table.className?uncap_first + "Alias">
	<#assign resultMap=table.className?uncap_first + "ResultMap">
	<#assign sysFields=["id","deleted"]>
	<resultMap id="${resultMap}" type="${beanAlias}" >
  	<#list table.fields as field>
    	<result column="${field.columnName}" property="${field.property}"/>
    </#list>
	</resultMap>
	
	<insert id="insert" parameterType="${beanAlias}" >
		insert into ${table.tableName}
		<trim prefix="(" suffix=")" prefixOverrides="," suffixOverrides=","> 
		<#list table.fields as field>
			${field.columnName},
		</#list>
		</trim>
		values
		<trim prefix="(" suffix=")" prefixOverrides="," suffixOverrides=","> 
		<#list table.fields as field>
			${r"#{"+field.property+"}"},
		</#list>
		</trim>
	</insert>
	
	<delete id="delete" parameterType="${beanAlias}" >
		delete from ${table.tableName}
		where ID = ${r"#{id}"} and MODIFY_TIME=${r"#{modifyTime}"}
	</delete>
	
	<delete id="deleteByParam" parameterType="map" >
		delete from ${table.tableName}
		<where>
		<include refid="allSql"/>
		</where>
	</delete>
	
	<update id="update" parameterType="${beanAlias}" >
		update ${table.tableName}
		<set> 
		<#list table.fields as field>
			<#if (field.property != "id") && (field.property != "modifyTime")>
		${field.columnName} = ${r"#{"+field.property+"}"},
			</#if>
	 	</#list>
		MODIFY_TIME = ${r"#{now}"}
		</set>
		where ID = ${r"#{id}"} and MODIFY_TIME=${r"#{modifyTime}"}
	</update>
	
	<sql id="allSql">
		<if test="entity!=null">
     <#list table.fields as field>
        <#if sysFields?seq_index_of(field.property) = -1>
			<if test="entity.${field.property}!=null">
  			<#if field.propertyType="Date">
				and date_format(${field.columnName},'%y-%m-%d') = date_format(${r"#{entity."+field.property+"}"},'%y-%m-%d')
  			<#elseif field.propertyType="String">
  				<if test="entity.${field.property}!=''">
				and ${field.columnName} = ${r"#{entity."+field.property+"}"}
				</if>
			<#else>
				and ${field.columnName} = ${r"#{entity."+field.property+"}"}
  			</#if>
			</if>
  		</#if>
	</#list>
		</if>
	</sql>
	
	<sql id="allSqlLike">
		<if test="entity!=null">
        <#list table.fields as field>
        <#if sysFields?seq_index_of(field.property) = -1>
			<if test="entity.${field.property}!=null">
  			<#if field.propertyType="Date">
				and date_format(${field.columnName},'%y-%m-%d') = date_format(${r"#{entity."+field.property+"}"},'%y-%m-%d')
  			<#elseif field.propertyType="String"> 
				<if test="entity.${field.property}!=''">
				and ${field.columnName} like ${r"#{entity."+field.property+"}"}
				</if>
  			</#if>
			</if>
  		</#if>
		</#list>
		</if>
	</sql>
	
	<select id="getById" resultMap="${resultMap}" parameterType="string" >
		select *
		from ${table.tableName}
		where ID = ${r"#{_parameter}"}
	</select>
	
	
	<select id="find" resultMap="${resultMap}" parameterType="map">
  		select * from ${table.tableName}
		<where> deleted='F'
		<include refid="allSql"/>
		</where>
		order by create_time desc
	</select>
	
	<select id="findLike" resultMap="${resultMap}" parameterType="map">
  		select * from ${table.tableName}
		<where> deleted='F'
		<include refid="allSqlLike"/>
		</where>
		order by create_time desc
	</select>
	
	<select id="page" resultMap="${resultMap}" parameterType="map" >
		<include refid="mybatis.pageStartSQL" />
		select * from ${table.tableName} 
		<where>	deleted='F'
		<include refid="allSql"/>
		</where>
		<include refid="mybatis.pageEndSQL" />
	</select>
	
	<select id="pageCount" resultType="map" parameterType="map" >
		select count(1) as COUNTER from ${table.tableName} 
		<where>	deleted='F'
		<include refid="allSql"/>
		</where>
		order by create_time desc
	</select>
	
	<select id="pageLike" resultType="map" parameterType="map">
		<include refid="mybatis.pageStartSQL" />
		select * from ${table.tableName} 
		<where> deleted='F'
		<include refid="allSqlLike"/>
		</where>
		order by create_time desc
		<include refid="mybatis.pageEndSQL" />
	</select>
  	<select id="pageLikeCount" resultType="map" parameterType="map">
		select count(1) as COUNTER from ${table.tableName} 
		<where> deleted='F'
		<include refid="allSqlLike"/>
		</where>
		order by create_time desc		
	</select>
  
  	<select id="getNewId" resultType="Long" parameterType="long">
		select func_seq_factory('TABLE_${table.tableName}',${r'#{_parameter}'})
	</select>
	
</mapper>